package com.scuec.groupfive.service.impl;

import com.scuec.groupfive.entity.FavoriteDetail_z;
import com.scuec.groupfive.mapper.FavoriteDetailMapper_z;
import com.scuec.groupfive.service.FavoriteDetailService_z;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FavoriteDetailServiceImpl_z implements FavoriteDetailService_z {
    private static final Logger logger = LoggerFactory.getLogger(FavoriteDetailServiceImpl_z.class);

    @Resource
    private FavoriteDetailMapper_z favoriteDetailMapper;

    @Override
    public List<FavoriteDetail_z> getFavoriteDetails(Long userId) {
        logger.info("Getting favorite details for user: {}", userId);
        List<FavoriteDetail_z> details = favoriteDetailMapper.getFavoriteDetails(userId);
        logger.info("Found {} favorite details for user {}", details.size(), userId);
        return details;
    }

    @Override
    public List<FavoriteDetail_z> getFavoriteQuestions(Long userId, Long folderId, int page, int size, String query) {
        int totalFavorites = favoriteDetailMapper.checkFavoriteCount(userId);
        int activeFavorites = favoriteDetailMapper.checkActiveFavoriteCount(userId);
        logger.info("User {} has {} total favorites and {} active favorites", userId, totalFavorites, activeFavorites);

        int offset = (page - 1) * size;
        List<FavoriteDetail_z> questions = favoriteDetailMapper.getFavoriteQuestions(userId, folderId, offset, size, query);
        logger.info("Retrieved {} questions for user {} (page: {}, size: {}, folderId: {}, query: '{}')",
                questions.size(), userId, page, size, folderId, query);

        // 日志输出第一条记录的详细信息用于调试
        if (!questions.isEmpty()) {
            FavoriteDetail_z firstQuestion = questions.get(0);
            logger.info("Sample question data - ID: {}, Year: {}, OriginalSequence: {}, SubjectName: {}, TypeId: {}",
                    firstQuestion.getQuestionId(), firstQuestion.getYear(),
                    firstQuestion.getOriginalSequence(), firstQuestion.getSubjectName(), firstQuestion.getTypeId());
        }

        return questions;
    }

    @Override
    public int getFavoriteQuestionsCount(Long userId, Long folderId, String query) {
        int count = favoriteDetailMapper.getFavoriteQuestionsCount(userId, folderId, query);
        logger.info("Total favorite questions count for user {}: {}", userId, count);
        return count;
    }

    @Override
    public int checkFavoriteCount(Long userId) {
        return favoriteDetailMapper.checkFavoriteCount(userId);
    }

    @Override
    public int checkActiveFavoriteCount(Long userId) {
        return favoriteDetailMapper.checkActiveFavoriteCount(userId);
    }

    @Override
    public void removeFavoriteQuestion(Long userId, Long questionId) {
        logger.info("Removing question {} from favorites for user {}", questionId, userId);
        favoriteDetailMapper.removeFavoriteQuestion(userId, questionId);
        logger.info("Successfully removed question {} from favorites for user {}", questionId, userId);
    }

    @Override
    public FavoriteDetail_z getFavoriteQuestionDetail(Long userId, Long questionId) {
        logger.info("Getting favorite question detail for user {} and question {}", userId, questionId);
        FavoriteDetail_z detail = favoriteDetailMapper.getFavoriteQuestionDetail(userId, questionId);
        if (detail != null) {
            logger.info("Found question detail - Year: {}, OriginalSequence: {}, SubjectName: {}, TypeId: {}",
                    detail.getYear(), detail.getOriginalSequence(), detail.getSubjectName(), detail.getTypeId());
        } else {
            logger.warn("No favorite question detail found for user {} and question {}", userId, questionId);
        }
        return detail;
    }
}